package wzh.crm.ui;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ScrollView;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

import wzh.crm.R;
import wzh.crm.bl.CURDBL;
import wzh.crm.vo.Vo;

/**
 * Created by wangzehao on 2016/5/25.
 */
public class AllAndMineListActivity <T extends Vo> extends AppCompatActivity {
    private static final int ALL=0;
    private static final int MINE=1;

    private int state;

    protected DisplayListView<T> list;
    protected ScrollView listContainer;
    protected CURDBL<T> bl;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_and_mine_list);

        AsyncTask<Object,Object,CURDBL<T>> asyncTask=new AsyncTask<Object, Object, CURDBL<T>>() {
            @Override
            protected CURDBL<T> doInBackground(Object... params) {
                return setInitBl();
            }
        };
        asyncTask.execute(new Object());
        try {
            bl=asyncTask.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        list= buildList();
        listContainer= (ScrollView) findViewById(R.id.display_list_container);
        putListIntoContainer();

        state=ALL;
    }

    @Override
    protected void onResume() {
        super.onResume();
        display();
    }

    private void display(){
        List<T> model;
        AsyncTask<Object, Object, List<T>> asyncTask=new AsyncTask<Object, Object, List<T>>() {

            @Override
            protected List<T> doInBackground(Object... params) {
                return getModelList();
            }
        };
        asyncTask.execute(new Object());
        try {
            model=asyncTask.get();
            list.setModel(model);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }

    private List<T> getModelList(){
        try{
        if(state==ALL){
            return bl.getAll();
        }else if(state==MINE){
            return bl.getMine(0);
        }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        return null;
    }

    public void displayAll(View view){
        state=ALL;
        display();
    }

    public void displayMine(View view){
        state=MINE;
        display();
    }

    private void putListIntoContainer(){
        listContainer.removeAllViews();
        listContainer.addView(list);
    }

    protected DisplayListView<T> buildList(){
        return (DisplayListView<T>)findViewById(R.id.display_list);
    }

    protected CURDBL<T> setInitBl(){
        return null;
    }
}
